<template>
  <div class="container">
    <base-title base-title="发布留言" />
    <textarea class="text" rows="10" v-model="wordContent" placeholder="这是一块留言板~" :maxlength="maxlength"></textarea>
    <div class="main">
      <Emoji @handle-emotion="handleEmotion" />
      <div class="font">
        <span>{{ wordContent.trim().length }}</span>
        <span>/</span>
        <span>{{ maxlength }}</span>
      </div>
      <div class="create">
        <el-button type="primary" @click="createWord">发布</el-button>
      </div>
    </div>
  </div>
</template>

<script lang="ts" name="wordEdit" setup>
  import notification from '@/utils/notification'
  import { createWordRequest } from '@/network/api/word-api'
  import { ref } from 'vue'
  const wordContent = ref('')
  const maxlength = ref(200)

  const handleEmotion = (value: string) => {
    wordContent.value += value
  }
  const createWord = async () => {
    if (!wordContent.value.trim()) return notification.error('留言内容不能为空~')
    if (wordContent.value.length > maxlength.value) return notification.warning(`留言最大字数不能超过${maxlength.value}~`)
    const result = await createWordRequest(wordContent.value)
    if (result.status === 200) {
      notification.success(result.msg)
      wordContent.value = ''
    }
  }
</script>

<style lang="less" scoped>
  .main {
    position: relative;
    .font {
      position: absolute;
      top: -40px;
      right: 12px;
      font-size: 14px;
      color: #999;
    }
    .create {
      position: absolute;
      top: -2px;
      right: 7px;
      .el-switch {
        margin: 0 10px;
      }
    }
  }
  .text {
    display: block;
    margin: 0 auto;
    margin-bottom: 10px;
    width: 100%;
    resize: none;
    box-sizing: border-box;
    padding: 5px 10px;
    border-radius: 8px;
    border: none;
    outline: none;
    font-size: 16px;
    background-color: #f0f1f4;
  }
</style>
